﻿#include "widget.h"
#include "ui_widget.h"
#include <QTcpServer>
#include <QDebug>
#include <QTcpSocket>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    m_tcpServer = new QTcpServer(this);
    m_tcpServer->listen(QHostAddress::Any, 80);
    connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(newConnectionSlot()));
    connect(m_tcpServer, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(errorStringSlot()));
}

Widget::~Widget()
{
    delete ui;
    m_tcpServer->close();
}

void Widget::newConnectionSlot()
{
    qDebug() << "newConnectionSlot() called!";
    m_tcpSocket = m_tcpServer->nextPendingConnection();
    connect(m_tcpSocket, SIGNAL(readyRead()), this, SLOT(sendMsg()));
}

void Widget::errorStringSlot()
{
    qDebug() << m_tcpServer->errorString();
}

void Widget::sendMsg()
{
    qDebug() << m_tcpSocket->readAll();

    QString contentStr = "<html>"
                         "<head>"
                         "<title>"
                         "Hello"
                         "</title>"
                         "</head>"
                         "<body>"
                         "<h1>Hello World</h1>"
                         "</body>"
                         "</html>";

    //send msg
    QString str = "HTTP/1.1 200 OK\r\n";
    str.append("Server:nginx\r\n");
    str.append("Content-Type:text/html;charset=UTF-8\r\n");
    str.append("Connection:keep-alive\r\n");
    str.append(QString("Content-Length:%1\r\n\r\n").arg(contentStr.size()));
    str.append(contentStr);
    qDebug() << str;
    m_tcpSocket->write(str.toStdString().c_str());
}
